﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using TimKen.BussLogic;
using TimKen.DataEntity;

namespace TimKen.Web.User
{
    public partial class ErrorAuthority : WebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CurrentUserName == null || CurrentUserName == "")
            {
                Alert("您必须登录！", "../Index.aspx");
            }
            WebUserManager manager = new WebUserManager();
            WebUser webUser = manager.GetModel(CurrentUserName);
            if (webUser == null)
                Response.Redirect("/Error.aspx", true);
            BindResources();
        }

        protected void BindResources()
        {
            btnRegister.Text = GetLangString("Register");
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            WebUserManager manager = new WebUserManager();
            WebUser webUser = manager.GetModel(CurrentUserName);

            if (webUser == null)
                Response.Redirect("/Error.aspx", true);

            if (webUser.IsApply)
            {
                Alert(GetLangString("HaveApply"));
                return;
            }

            try
            {
                if (this.fileBusinessLicenseCopy.FileBytes.Length == 0 || !this.fileBusinessLicenseCopy.HasFile)
                {
                    Alert(GetLangString("x_BusinessLicenseCopyNotAllowBlank"));
                    return;
                }

                if (this.fileTaxRegistrationCopy.FileBytes.Length == 0 || !this.fileTaxRegistrationCopy.HasFile)
                {
                    Alert(GetLangString("x_TaxRegistrationCopyNotAllowBlank"));
                    return;
                }


                string extensionBusinessLicenseCopy = System.IO.Path.GetExtension(fileBusinessLicenseCopy.FileName).ToLower();

                string extensionTaxRegistrationCopy = System.IO.Path.GetExtension(fileTaxRegistrationCopy.FileName).ToLower();

                //上传文件的格式验证
                string[] allowedExtensions = { ".gif", ".jpg", ".png", ".bmp", };
                Array.Sort(allowedExtensions);
                if (
                    Array.BinarySearch(allowedExtensions,
                                        extensionBusinessLicenseCopy) < 0)
                {
                    Alert(GetLangString("InputInvalid"));
                    return;
                }

                if (
                    Array.BinarySearch(allowedExtensions,
                                       extensionTaxRegistrationCopy) < 0)
                {
                    Alert(GetLangString("InputInvalid"));
                    return;
                }

                string BLCfileName = "BLC" + System.Guid.NewGuid().ToString() +
                                     extensionBusinessLicenseCopy;
                string TRCfileName = "TRC" + System.Guid.NewGuid().ToString() +
                                     extensionTaxRegistrationCopy;


                this.fileBusinessLicenseCopy.SaveAs(PhysicalUploadFilePathForUser + BLCfileName);
                this.fileTaxRegistrationCopy.SaveAs(PhysicalUploadFilePathForUser + TRCfileName);


                webUser.IsApply = true;
                webUser.ApplyDate = DateTime.Now;

                webUser.BusinessLicenseCopy = BLCfileName;
                webUser.TaxRegistrationCopy = TRCfileName;

                webUser.DeliveryAddress = this.txtDeliveryAddress.Text;
                webUser.DeliveryLinkMan = this.txtDeliveryLinkMan.Text;

                webUser.ShippingAddress = this.txtShipAddress.Text;
                webUser.ShippingLinkMan = this.txtShippingLinkMan.Text;

                manager.Update(webUser);


                //给管理员发邮件通知
                Tools.SendApplyForCustomerEmail(webUser.UserName);


                Alert(GetLangString("ApplyOK"));
            }
            catch
            {
                Alert(GetLangString("ApplyError"));
            }
            finally
            {
            }

        }
    }
}